package tree_util;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * 上传照片
 * */
public class UpLoadPicture {
	
	/**
	 * @param Photo 文件引用
	 * @param PhotoFileName 文件名
	 * @param new_name_start   表的ID(开头三个字母加上时间的不同就能唯一定位一张照片)
	 * 
	 * @return	所有的照片路径   例：  MON20160501102201_1.jpg|MON20160501102201_2.png
	 * */
	public static String upload(List<File> Photo,List<String> PhotoFileName,
			String new_name_start){
		String photo_name = "";				//返回的路径
		System.out.println("loadPhoto");
		File local = null;			//本地照片文件
    	File server = null;			//对应的存储在服务器的文件夹
    	String server_path1 = ServletActionContext.getServletContext().getRealPath("/picture");
    	System.out.println(server_path1);
    	File server1 = new File(server_path1);
		if(!server1.exists()){
			server1.mkdir();
		}
		String server_path = ServletActionContext.getServletContext().getRealPath("/picture")+"/"+new_name_start;

		server = new File(server_path);
		if(!server.exists()){
			server.mkdir();
		}
		else{
			//如果检测到该表已经有照片存在,删除
			File[] file = server.listFiles();
			for(int t = 0;t < file.length; t++){		
			  	photo_name+=file[t].getName()+"|";
			}
		}
		
		File file = null;
		int start = server.listFiles().length;
		System.out.println("照片数量："+Photo.size());
		for(int i=0;i<Photo.size();i++){
			//下面这条语句不行，，原因是可能照片名字就包含".",只能从后往前遍历出"."的位置
			//int m = PhotoFileName.get(i).indexOf(".");
			int m = PhotoFileName.get(i).length()-1;
			boolean flag = false;						//标记"."是否找到
			while (!flag) {
				if(PhotoFileName.get(i).substring(m, m+1).equals(".")){
					flag = true;
				}else{
					m --;
				}
			}
			String type = PhotoFileName.get(i).substring(m+1);
			
			String child =i+start+ "." +type;
			file = new File(server_path, child);
			
			try {
				FileUtils.copyFile(Photo.get(i), file);
				
				photo_name += (child + "|");
				
			} catch (IOException e) {
				// TODO Auto-generated catch block
				System.out.println("照片复制错误");
				e.printStackTrace();
				return "photo_error";
			}
		}
	   
		if(photo_name.length()!=0)
		photo_name = photo_name.substring(0,photo_name.length()-1);
		
		return photo_name;
	}
	
	public static String Delete(String NodeName,String deletePhoto){
		String server_path=ServletActionContext.getServletContext().getRealPath("/picture")+"/"+NodeName;
		File delete = new File(server_path+"/"+deletePhoto);
		delete.delete();
		return  getAllPhoto(NodeName);
		/*
		String path="";
		File server = new File(server_path);          
		File[] re = server.listFiles();
		for(int i = 0 ;i<server.listFiles().length;i++){
			int m =re[i].getName().length()-1;
			System.out.print(re[i].getName());
			boolean flag = false;						//标记"."是否找到
			while (!flag) {
				if(re[i].getName().substring(m, m+1).equals(".")){
					flag = true;
				}else{
					m --;
				}
			}
			
			String type = re[i].getName().substring(m+1);
			String child = i+"."+type;
			path+=child+"|";
		    File reName = new File(server_path,child);
			re[i].renameTo(reName);
		}
		if(path.length()!=0)
        return path.substring(0,path.length()-1);
		else return path;
		*/
	}
	
	public static String getAllPhoto(String NodeName){
		String server_path=ServletActionContext.getServletContext().getRealPath("/picture")+"/"+NodeName;
		String path="";
		System.out.println("000000000000000");
		File server = new File(server_path);
		if(!server.exists() || server.listFiles().length <= 0) {
			System.out.println("1111111"+server.exists());
			server.delete();
			return path;
		}		
		else {
			System.out.println("2222222"+server.exists());
			File[] re = server.listFiles();
			for(int i = 0 ;i<server.listFiles().length;i++){
				int m =re[i].getName().length()-1;
				System.out.print(re[i].getName());
				boolean flag = false;						//标记"."是否找到
				while (!flag) {
					if(re[i].getName().substring(m, m+1).equals(".")){
						flag = true;
					}else{
						m --;
					}
				}
				String type = re[i].getName().substring(m+1);
				String child = i+"."+type;
				path+=child+"|";
			    File reName = new File(server_path,child);
				re[i].renameTo(reName);
			}
			return path.substring(0,path.length()-1);
		}
	}
	
	public static void DeleteAll(String Name){
		String server_path1=ServletActionContext.getServletContext().getRealPath("/picture");
		System.out.println("aaaaaaaaaaaaaaa"+Name);
        File server1 = new File(server_path1);
		System.out.println("bbbbbbbaa");

		if(!server1.exists()){
			return;
		}
		String server_path2=ServletActionContext.getServletContext().getRealPath("/picture/"+Name);
        File server2 = new File(server_path2);
        if(!server2.exists()){
			return;
		}
    	File[] file = server2.listFiles();
    	
		for(int t = 0;t < file.length; t++){
				file[t].delete();
					
		}
		
        server1.delete();
	}

}
